using System;
using System.Globalization;

namespace NGCDS
{
    public class RemoteFileInfo
    {
        // column separator
        private const char SEPARATOR = '\t';

        // C# 3.0 New Feature
        public string FileName { get; private set; }
        public string DateTimeString { get; private set; }
        public long Length { get; private set; }

        public RemoteFileInfo(string filename, string datetimeString, long size)
        {
            init(filename, datetimeString, size);
        }

        public RemoteFileInfo(string filename, DateTime datetime, long size)
        {
            init(filename, datetime.ToString("yyyyMMddHHmmss"), size);
        }

        public RemoteFileInfo(string fileInfoString)
        {
            if (fileInfoString.Contains(SEPARATOR.ToString()))
            {
                string[] item = fileInfoString.Split(SEPARATOR);
                init(item[0], item[1], Convert.ToInt64(item[2]));
            }
        }

        private void init(string filename, string datetimeString, long length)
        {
            this.FileName = filename;
            this.DateTimeString = datetimeString;
            this.Length = length;
        }

        public override string ToString()
        {
            return string.Format("{0} {1} {2}", FileName, Length, DateTimeString);
        }

        public string ToStringHumanReadable()
        {
            string sizeString = null;

            // 1258291 = 1024 * 1024 * 1.2
            if (Length > 1258291)
            {
                // 1048576 = 1024 * 1024
                sizeString = string.Format("{0:0.00}MB", 1.0 * Length / 1048576);
            }
            else if (Length > 1024 * 1.2)
            {
                sizeString = string.Format("{0:0.00}KB", 1.0 * Length / 1024);
            }
            else
            {
                sizeString = string.Format("{0}B", Length);
            }

            CultureInfo ci = new CultureInfo("zh-TW", true);
            DateTime dt = DateTime.ParseExact(DateTimeString, "yyyyMMddHHmmss", ci);
            return string.Format("{0} {1,20} {2}", dt.ToString("yyyy-MM-dd HH:mm"), sizeString, FileName);
        }
    }
}
